﻿using UnityEngine;
using System.Collections.Generic;
using System;
using TTGame;
/*
 * SkillRangeSelector
 * 技能施法范围
 *
 * 保存技能施法范围参数和选择后的结果。
 * 提供[某点是否在范围内]的接口，用来搜索对象列表。
 *
 *
 * 依赖：
 * Puppet
 *
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/17/2017 2:06:33 PM
 */
namespace TTGameEngine {
public enum SkillSelectorType {
    Sector,// 扇型
    Direct,// 方向型（矩形）
    Area,// 范围型
    Lock,// 选择型（锁定单个对象）
    SelfCenter,// 自身或自身范围，不需要手动选择目标
}

[Serializable]
public class SkillSelector {
    [SerializeField]
    SkillSelectorType m_selectorType;
    /// <summary>
    ///             预设参数              选择结果
    /// 扇形型参数：选择范围，夹角        原点，方向
    /// 方向型参数：选择范围, 宽度        原点，方向
    /// 范围型参数：选择范围，作用半径    中心点
    /// 选择型参数：选择范围              对象
    /// 自身范围参数：选择范围            原点
    /// </summary>
    [SerializeField]
    float m_selectorRadius;
    [SerializeField]
    float m_angle;
    [SerializeField]
    float m_width;
    [SerializeField]
    float m_hitRadius;
    [SerializeField]
    Vector3 m_resultTargetPos;
    [SerializeField]
    Vector3 m_resultDir;
    [SerializeField]
    string m_resultLockObjGID;
    [SerializeField]
    Vector3 m_resultSelfPos;
    [SerializeField]
    Puppet m_owner;

    bool m_isSelectedEnd = false;
    SelectorStateID m_selectorResult = SelectorStateID.UnActive;
    public SelectorStateID SelectorState {
        get {
            return m_selectorResult;
        }
    }

    public bool IsSelected {
        get { return m_isSelectedEnd; }
    }

    public SkillSelectorType SelectorType {
        get { return m_selectorType; }
    }
    public float SelectorRadius {
        get { return m_selectorRadius; }
    }

    public Vector3 ResultSelfPos {
        get {return m_resultSelfPos;}
    }
    public Vector3 ResultHitCenter {
        get { return m_resultTargetPos; }
    }
    public Vector3 ResultDir {
        get { return m_resultDir; }
    }
    public string ResultLockObjGID {
        get { return m_resultLockObjGID; }
    }
    public Puppet OwnerObj {
        get { return m_owner; }
    }

    public void SetOwnerObj(Puppet owner) {
        m_owner = owner;
    }

    #region 初始化选择器
    public void InitSelector_Sector(SkillSelectorType selType, float selectRange, float angle) {
        if(selType != SkillSelectorType.Sector) {
            DebugUtil.LogErrorFormat("[SkillSelector] InitSelector_Sector. TypeCheckError:[{0}]", selType.ToString());
        }
        m_selectorType = selType;
        m_selectorRadius = selectRange;
        m_angle = angle;
        m_selectorResult = SelectorStateID.Activing;
    }

    public void InitSelector_Direct(SkillSelectorType selType, float selectRange, float width) {
        if(selType != SkillSelectorType.Direct) {
            DebugUtil.LogErrorFormat("[SkillSelector] InitSelector_Direct. TypeCheckError:[{0}]", selType.ToString());
        }
        m_selectorType = selType;
        m_selectorRadius = selectRange;
        m_width = width;
        m_isSelectedEnd = false;
        m_selectorResult = SelectorStateID.Activing;
    }

    public void InitSelector_Area(SkillSelectorType selType, float selectRange, float radius) {
        if(selType != SkillSelectorType.Area) {
            DebugUtil.LogErrorFormat("[SkillSelector] InitSelector_Area. TypeCheckError:[{0}]", selType.ToString());
        }
        m_selectorType = selType;
        m_selectorRadius = selectRange;
        m_hitRadius = radius;
        m_selectorResult = SelectorStateID.Activing;
    }

    public void InitSelector_Lock(SkillSelectorType selType, float selectRange) {
        if(selType != SkillSelectorType.Lock) {
            DebugUtil.LogErrorFormat("[SkillSelector] InitSelector_Lock. TypeCheckError:[{0}]", selType.ToString());
        }
        m_selectorType = selType;
        m_selectorRadius = selectRange;
        m_selectorResult = SelectorStateID.Activing;
    }

    public void InitSelector_SelfCenter(SkillSelectorType selType, float selectRange) {
        if(selType != SkillSelectorType.SelfCenter) {
            DebugUtil.LogErrorFormat("[SkillSelector] InitSelector_SelfCenter. TypeCheckError:[{0}]", selType.ToString());
        }
        m_selectorType = selType;
        m_selectorRadius = selectRange;
        m_selectorResult = SelectorStateID.Activing;
    }
    #endregion

    #region 选择器设置结果
    public void SetResult_Sector(SkillSelectorType selType, Vector3 direct) {
        if(selType != SkillSelectorType.Sector) {
            DebugUtil.LogErrorFormat("[SkillSelector] SetResult_Sector. TypeCheckError:[{0}]", selType.ToString());
        }
        m_resultSelfPos = m_owner.objTF.position;
        m_resultDir = direct.normalized;
        m_selectorResult = SelectorStateID.SelectOK;
        endSelector();
    }

    public void SetResult_Direct(SkillSelectorType selType, Vector3 direct) {
        if(selType != SkillSelectorType.Direct) {
            DebugUtil.LogErrorFormat("[SkillSelector] SetResult_Direct. TypeCheckError:[{0}]", selType.ToString());
        }
        m_resultSelfPos = m_owner.objTF.position;
        m_resultDir = direct.normalized;
        m_selectorResult = SelectorStateID.SelectOK;
        m_resultTargetPos = m_resultSelfPos;
        endSelector();
    }

    public void SetResult_Area(SkillSelectorType selType, Vector3 centerPos) {
        if(selType != SkillSelectorType.Area) {
            DebugUtil.LogErrorFormat("[SkillSelector] SetResult_Area. TypeCheckError:[{0}]", selType.ToString());
        }
        m_resultSelfPos = m_owner.objTF.position;
        m_resultTargetPos = centerPos;
        m_selectorResult = SelectorStateID.SelectOK;
        m_resultDir = (centerPos - m_resultSelfPos).normalized;
        endSelector();
    }

    public void SetResult_Lock(SkillSelectorType selType, string objGID, Vector3 targetObjPos) {
        if(selType != SkillSelectorType.Lock) {
            DebugUtil.LogErrorFormat("[SkillSelector] SetResult_Lock. TypeCheckError:[{0}]", selType.ToString());
        }
        m_resultSelfPos = m_owner.objTF.position;
        m_resultLockObjGID = objGID;
        m_selectorResult = SelectorStateID.SelectOK;
        m_resultDir = m_owner.objTF.forward;
        m_resultTargetPos = targetObjPos;
        endSelector();
    }

    public void SetResult_SelfCenter(SkillSelectorType selType) {
        if(selType != SkillSelectorType.SelfCenter) {
            DebugUtil.LogErrorFormat("[SkillSelector] SetResult_SelfCenter. TypeCheckError:[{0}]", selType.ToString());
        }
        m_resultSelfPos = m_owner.objTF.position;
        m_selectorResult = SelectorStateID.SelectOK;
        m_resultDir = m_owner.objTF.forward;
        m_resultTargetPos = m_resultSelfPos;
        endSelector();
    }

    public void Cancel() {
        m_selectorResult = SelectorStateID.SelectCancel;
        endSelector();
    }

    void endSelector() {
    }
    #endregion

    public bool IsInRange(Vector3 pos) {
        bool result = false;
        switch(m_selectorType) {
            case SkillSelectorType.Sector:// 扇型
                result = Tools.IsPointInCircularSector(pos, m_resultSelfPos, m_resultDir, m_selectorRadius, m_angle);
                break;
            case SkillSelectorType.Direct:// 方向型
                result = Tools.IsPointInRect2D(pos, m_resultSelfPos, m_resultDir, m_width, m_selectorRadius);
                break;
            case SkillSelectorType.Area:// 范围型
                result = Tools.IsPointInCircle(pos, m_resultTargetPos, m_hitRadius);
                break;
            case SkillSelectorType.Lock:// 选择型
                DebugUtil.LogErrorFormat("[SkillSelector] Lock Type don't need Check Range");
                break;
            case SkillSelectorType.SelfCenter:// 自身范围
                result = Tools.IsPointInCircle(pos, m_resultSelfPos, m_selectorRadius);
                break;
        }
        return result;
    }



}
}